﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace Example7_Collisions.View
{
    class View 
    {
        private SpriteBatch     m_spriteBatch;
        private Texture2D       m_tileTexture;
        private Texture2D       m_playerTexture;

        private KeyboardState   m_oldKeyboardState;
        
        
        //note that the view scale is less than texture scale
        private int             m_textureTileSize = 64;

        public View(GraphicsDeviceManager a_manager, ContentManager a_contentLoader)
        {
            //this is needed to draw sprites
            m_spriteBatch = new SpriteBatch(a_manager.GraphicsDevice);

            //Load content
            m_tileTexture = a_contentLoader.Load<Texture2D>("tiles");
            m_playerTexture = a_contentLoader.Load<Texture2D>("player");
            
        }

        
        public void DrawLevel(GraphicsDevice a_graphicsDevice, Model.Level a_level, Camera a_camera, Vector2 a_playerPosition)
        {
            Vector2 viewportSize = new Vector2(a_graphicsDevice.Viewport.Width, a_graphicsDevice.Viewport.Height);
            float scale = a_camera.GetScale();

            a_graphicsDevice.Clear(Microsoft.Xna.Framework.Color.Green);

            //draw all images
            m_spriteBatch.Begin();
            
            //draw level
            for (int x = 0; x < Model.Level.g_levelWidth; x++)
            {
                for (int y = 0; y < Model.Level.g_levelHeight; y++)
                {
                    Vector2 viewPos = a_camera.GetViewPosition(x, y, viewportSize);
                    DrawTile(viewPos.X, viewPos.Y, scale, a_level.m_tiles[x, y]);
                }
            }

            //draw player on top of level
            Vector2 playerViewPos = a_camera.GetViewPosition(a_playerPosition.X, a_playerPosition.Y, viewportSize);
            DrawPlayerAt(playerViewPos, scale); 
            m_spriteBatch.End();
        }

        

        private void DrawPlayerAt(Microsoft.Xna.Framework.Vector2 a_viewBottomCenterPosition, float a_scale)
        {

           

            //Create rectangle and draw it, note the transformation of the position
            Rectangle destRect = new Rectangle((int)(a_viewBottomCenterPosition.X - a_scale/2.0f), (int)(a_viewBottomCenterPosition.Y - a_scale), (int)a_scale, (int)a_scale);
            m_spriteBatch.Draw(m_playerTexture, destRect, Color.White);
        }

        private void DrawTile(float a_x, float a_y, float a_scale, Model.Tile a_tile)
        {
            int textureIndex = a_tile.isBlocked() ? 1 : 0;



            //Get the source rectangle (pixels on the texture) for the tile type 
            Rectangle sourceRectangle = new Rectangle(m_textureTileSize * textureIndex, 0, m_textureTileSize, m_textureTileSize);

            //Destination rectangle in windows coordinates only scaling
            Rectangle destRect = new Rectangle((int)a_x, (int)a_y, (int)a_scale, (int)a_scale);


            m_spriteBatch.Draw(m_tileTexture, destRect, sourceRectangle, Color.White);
        }


        public bool DidPlayerPressJump()
        {
            Keys jumpKey = Keys.Space;
            
            KeyboardState newState = Keyboard.GetState();
            //save state
            m_oldKeyboardState = newState;

            if (m_oldKeyboardState.IsKeyDown(jumpKey))
            {
                return true;
            }
            return false;
        }

        internal bool DidPlayerPressLeft()
        {
            return m_oldKeyboardState.IsKeyDown(Keys.Left);
        }

        internal bool DidPlayerPressRight()
        {
            return m_oldKeyboardState.IsKeyDown(Keys.Right);
        }
    }
}
